﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
namespace WPFExcelReport.ViweModels
{
    public class WFSUBSCRIBERViewModel:BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return app.Mandant;
            }
        }
        public string company
        {
            get
            {
                return app.company;
            }
        }
        public string UserID
        {
            get
            {
                return app.User;
            }
        }
        bool IsSetEnvUpdate = true;
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
 
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public WFSUBSCRIBERViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                if (INTEXDBC == null)
                {
                    return setReturn;
                }
                DataRow dwData = SetEnv.ICC_WFEVENTSGROUP.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_WFEVENTSGROUP(dwData, 4);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFTDES"] = Convert8859P1ToGB2312(row["WFTDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_WFEVENTSCLASS.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_WFEVENTSCLASS(dwData, 4);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFCDES"] = Convert8859P1ToGB2312(row["WFCDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.ICC_WFEVENTDEF.NewRow();
                dwData["MANDANT"] = Mandant;
                setResult = INTEXDBC.QueryICC_WFEVENTDEF(dwData, 4);
                foreach (DataRow row in setResult.Tables[0].Rows)
                {
                    row["WFEDES"] = Convert8859P1ToGB2312(row["WFEDES"].ToString());
                }
                setReturn.Merge(setResult);
                dwData = SetEnv.HRSTAMM.NewRow();
                dwData["HRMANDANT"] = Mandant;
                setResult = INTEXDBC.QueryHRSTAMM(dwData, 4); 
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet Load()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                if(IsSetEnvUpdate)
                {
                    SetEnv.Clear();
                    SetEnv.Merge(LoadEnv());
                }
                IsSetEnvUpdate=false;
                dwData = SetEnv.ICC_WFSUBSCRIBER.NewRow();
                setResult = INTEXDBC.QueryICC_WFSUBSCRIBER(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void Save()
        {
            try
            {
                foreach (DataRow rw in SetDetail.ICC_WFSUBSCRIBER.Rows)
                {
                    INTEXDBC.SaveICC_WFSUBSCRIBER(rw);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete(DataRow dwInput)
        {
            try
            {
                INTEXDBC.DeleteICC_WFSUBSCRIBER(dwInput, 1);
                DataRow[] rows;
                rows = SetMaster.ICC_WFSUBSCRIBER.Select("ROWNID = '" + dwInput["ROWNID"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
                rows = SetDetail.ICC_WFSUBSCRIBER.Select("ROWNID = '" + dwInput["ROWNID"].ToString() + "'");
                foreach (DataRow rw in rows)
                {
                    rw.Delete();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
